package com.portal.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.portal.backend.exception.ResourceNotFoundException;
import com.portal.backend.model.User;
import com.portal.backend.payload.SignUpRequest;
import com.portal.backend.payload.UserProfileUpdateRequest;
import com.portal.backend.payload.UserSummary;
import com.portal.backend.mapper.UserMapper;
import com.portal.backend.security.UserPrincipal;
import com.portal.backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public User createUser(SignUpRequest signUpRequest) {
        User user = new User();
        user.setUsername(signUpRequest.getUsername());
        user.setEmail(signUpRequest.getEmail());
        user.setPassword(passwordEncoder.encode(signUpRequest.getPassword()));
        user.setFullName(signUpRequest.getFullName());
        user.setRole("USER");
        user.setEnabled(true);
        userMapper.insert(user);
        return user;
    }

    @Override
    public User getCurrentUser() {
        UserPrincipal userPrincipal = (UserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userMapper.selectById(userPrincipal.getId());
        if (user == null) throw new ResourceNotFoundException("User", "id", userPrincipal.getId());
        return user;
    }

    @Override
    public UserSummary getCurrentUserSummary() {
        UserPrincipal userPrincipal = (UserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userMapper.selectById(userPrincipal.getId());
        if (user == null) throw new ResourceNotFoundException("User", "id", userPrincipal.getId());
        return new UserSummary(user.getId(), user.getUsername(), user.getFullName(), user.getAvatar());
    }

    @Override
    public User getUserById(Long id) {
        User user = userMapper.selectById(id);
        if (user == null) throw new ResourceNotFoundException("User", "id", id);
        return user;
    }

    @Override
    public User getUserByUsername(String username) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        if (user == null) throw new ResourceNotFoundException("User", "username", username);
        return user;
    }

    @Override
    public User getUserByEmail(String email) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("email", email));
        if (user == null) throw new ResourceNotFoundException("User", "email", email);
        return user;
    }

    @Override
    public boolean existsByUsername(String username) {
        Long count = userMapper.selectCount(new QueryWrapper<User>().eq("username", username));
        return count != null && count > 0;
    }

    @Override
    public boolean existsByEmail(String email) {
        Long count = userMapper.selectCount(new QueryWrapper<User>().eq("email", email));
        return count != null && count > 0;
    }

    @Override
    public User updateUserProfile(UserProfileUpdateRequest profileUpdateRequest) {
        UserPrincipal userPrincipal = (UserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userMapper.selectById(userPrincipal.getId());
        if (user == null) throw new ResourceNotFoundException("User", "id", userPrincipal.getId());

        if (profileUpdateRequest.getFullName() != null && !profileUpdateRequest.getFullName().isEmpty()) {
            user.setFullName(profileUpdateRequest.getFullName());
        }
        if (profileUpdateRequest.getAvatar() != null) {
            user.setAvatar(profileUpdateRequest.getAvatar());
        }
        userMapper.updateById(user);
        return user;
    }
} 